package com.wisdom.benefit.jufa.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.wisdom.benefit.jufa.utils.DisplayUtil;

public class ProgressView extends View {

    private String[] mStatusArray;
    private Paint mPaint;
    private TextPaint mTextPaint;
    private int mColorBlue = Color.parseColor("#3586F9");
    private int mColorGray = Color.parseColor("#D6E6FE");
    private int mStartX;
    private int mStartY;
    private int mLineLength;
    private int mBluePointNum;

    public ProgressView(Context context) {
        this(context, null);
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);

        mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setTextSize(DisplayUtil.sp2px(getContext(), 13));
        mTextPaint.setColor(Color.parseColor("#333333"));
    }

    public void setStatusArray(@NonNull String[] statusArray, int bluePointNum) {
        mStatusArray = statusArray;
        mBluePointNum = bluePointNum;
        postInvalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldW, int oldH) {
        super.onSizeChanged(w, h, oldW, oldH);
        mStartX = w / 8;
        mStartY = h / 3;
        mLineLength = w * 3 / 4;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mStatusArray == null) return;
        //先画灰线
        mPaint.setStrokeWidth(DisplayUtil.dip2px(getContext(), 3));
        mPaint.setColor(mColorGray);
        canvas.drawLine(mStartX, mStartY, mStartX + mLineLength, mStartY, mPaint);
        //再画蓝线
        mPaint.setColor(mColorBlue);
        int length = mBluePointNum < mStatusArray.length ?
                mLineLength / (mStatusArray.length - 1) * (mBluePointNum - 1) + mLineLength / (mStatusArray.length - 1) / 2 : mLineLength;
        canvas.drawLine(mStartX, mStartY, mStartX + length, mStartY, mPaint);

        mPaint.setStrokeWidth(1);
        for (int i = 0; i < mStatusArray.length; i++) {
            int x = mStartX + mLineLength / (mStatusArray.length - 1) * i;
            //绘制大圆
            mPaint.setColor(mColorGray);
            canvas.drawCircle(x, mStartY, DisplayUtil.dip2px(getContext(), 7), mPaint);
            //绘制小圆
            mPaint.setColor(i < mBluePointNum ? mColorBlue : Color.WHITE);
            canvas.drawCircle(x, mStartY, DisplayUtil.dip2px(getContext(), 3), mPaint);
            //绘制文字
            canvas.drawText(mStatusArray[i], x, mStartY * 2, mTextPaint);
        }
    }
}
